// pages/doctor/doctor.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    // 供求
    page: 2,
    catId: 0,
    proData: [],
    cat: [],
    sortlist: [],
    sorttype: '',
    keyword: '',
    no0: false,
    no1: false,
    no2: false,
    tabArr: {
      curHdIndex: 0,
      curBdIndex: 0,
    },
    tab: 0,
    bei: false,
    catname: '分类专科',
    choose: '智能筛选',
  },

  // tab切换
  tabFun: function (e) {
    //获取触发事件组件的dataset属性 
    var _datasetId = e.target.dataset.id;
    console.log("----" + _datasetId + "----");
    if (_datasetId == 0) {
      this.setData({
        no0: true,
        no1: false,
        no2: false,
        tab: 1
      })
    }
    if (_datasetId == 1) {
      this.setData({
        no1: true,
        no0: false,
        no2: false,
        tab: 1
      })
    }
    if (_datasetId == 2) {
      this.setData({
        no2: true,
        no1: false,
        no0: false,
        tab: 1
      })
    }
    var _obj = {};
    _obj.curHdIndex = _datasetId;
    _obj.curBdIndex = _datasetId;
    this.setData({
      tabArr: _obj,
      bei: true,
      tab: 1
    })
  },
  kk: function () {
    this.setData({
      no0: false,
      no1: false,
      no2: false, tab: 0
    });
  },
  //筛选项点击操作
  //筛选项点击操作
  filter0: function (e) {
    var that = this;
    var _dataset = e.currentTarget.dataset.id;
    var txt = e.currentTarget.dataset.txt;
    that.setData({
      catname: txt,
      catId: _dataset,
    });
    that.loadProData();
  },

  filter1: function (e) {
    var that = this
    var stype = e.currentTarget.dataset.stype;
    var txt = e.currentTarget.dataset.txt;
    that.setData({
      choose: txt,
      sorttype: stype,
    });
    that.loadProData();
  },

  //跳转医生详情页面
  bindViewTap : function(e){
    var that = this;
    var docId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../detail/detail?docId=' + docId,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var keyword = options.keyword;
    that.setData({
      keyword: keyword,
    });
  },

  /**
   * * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    wx.request({
      url: app.api.hostUrl + '/Api/Doctor/lists',
      method: 'post',
      data: {
        uid: app.api.userId
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        var pro = res.data.pro;
        var cat = res.data.cat;
        var sortlist = res.data.sortlist;
        that.setData({
          proData: pro,
          cat: cat,
          sortlist: sortlist,
        });
      },
      fail: function (e) {
        wx.showToast({
          title: '网络异常！',
          duration: 2000
        });
      },
    })
  },

  /**
   * 根据筛选条件
   */
  loadProData: function () {
    var that = this;
    wx.request({
      url: app.api.hostUrl + '/Api/Doctor/sortlist',
      method: 'post',
      data: {
        uid: app.api.userId,
        cat_id: that.data.catId,
        sorttype: that.data.sorttype,
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        var pro = res.data.pro;
        that.setData({
          proData: pro,
        });
      },
      fail: function (e) {
        wx.showToast({
          title: '网络异常！',
          duration: 2000
        });
      },
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    var page = that.data.page;
    wx.request({
      url: app.api.hostUrl + '/Api/Doctor/get_more',
      method: 'post',
      data: {
        uid: app.api.userId,
        cat_id: that.data.catId,
        sorttype: that.data.sorttype,
        page: page,
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        var pro = res.data.pro;
        if (pro=='') {
          wx.showToast({
            title: '没有找到更多数据！',
            duration: 2000
          });
          return false;
        }
        that.setData({
          proData: that.data.proData.concat(pro),
          page: parseInt(page)+1,
        });
      },
      fail: function (e) {
        wx.showToast({
          title: '网络异常！',
          duration: 2000
        });
      },
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})